
//EditorWindow类无法打包,注意添加这个判断
#if UNITY_EDITOR

using UnityEngine;
using UnityEditor;
using System.Linq;
namespace PluginWD
{
    public class AddBoxCollider : EditorWindow
    {
        [MenuItem("Tools/添加移除BoxCollider")]

        public static void Open()
        {
            EditorWindow.GetWindow(typeof(AddBoxCollider));
        }
        void OnGUI()
        {
            if (GUILayout.Button("添加BoxCollider"))
            {
                Add();
            }

            if (GUILayout.Button("移除BoxCollider"))
            {
                Remove();
            }
        }
        public static void Remove()
        {
            foreach (var t in Resources.FindObjectsOfTypeAll(typeof(MeshRenderer)).Cast<MeshRenderer>())
            {
                HighlightableObject ho = t.gameObject.GetComponent<HighlightableObject>();
                if (ho != null)
                {
                    DestroyImmediate(ho, true);
                }
                BoxCollider bc = t.gameObject.GetComponent<BoxCollider>();
                if (bc != null)
                {
                    DestroyImmediate(bc, true);
                }
                EditorUtility.SetDirty(t); //刷新场景
                Debug.Log(t.gameObject.name);
            }
        }

        public static void Add()
        {
            foreach (var t in Resources.FindObjectsOfTypeAll(typeof(MeshRenderer)).Cast<MeshRenderer>())
            {
                // HighlightableObject ho = t.gameObject.GetComponent<HighlightableObject>();
                // if (ho == null)
                // {
                //     t.gameObject.AddComponent<HighlightableObject>();
                // }
                BoxCollider bc = t.gameObject.GetComponent<BoxCollider>();
                if (bc == null)
                {
                    t.gameObject.AddComponent<BoxCollider>();
                }
                EditorUtility.SetDirty(t);
                Debug.Log(t.gameObject.name);
            }
        }
    }
}
#endif